package com.atao.baokede.controller;

import com.atao.baokede.pojo.ChargeOrder;
import com.atao.baokede.pojo.Result;
import com.atao.baokede.pojo.vo.ChargeOrderVo;
import com.atao.baokede.pojo.vo.ChargeOrderVo2;
import com.atao.baokede.service.ChargeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 *  充值订单层
 **/
@RestController
@RequestMapping("/chargeOrder")
public class ChargeOrderController {

    @Autowired
    private ChargeOrderService chargeOrderService;

    /**
     * 添加充值订单
     * @param amount
     * @return
     */
    @PostMapping("/add/{amount}")
    public Result<ChargeOrderVo> addChargeOrder(@PathVariable BigDecimal amount){
        //检查amount是否为2位小数及以内小数位，如果大于两位小数或者不是浮点数，则报错
        if (!(amount.scale() <= 2 )) {
            return Result.error("金额小数位必须在2位以内");
        }
        return Result.success(chargeOrderService.add(amount));
    }

    /**
     * 查询所有充值订单
     * @return 充值订单列表
     */
    @GetMapping("/findAll")
    public Result<List<ChargeOrderVo2>> findAll() {
        return Result.success(chargeOrderService.findAll());
    }

    /**
     * 删除充值订单
     * @param orderNo 订单编号
     * @return 无
     */
    @DeleteMapping("/delete/{orderNo}")
    public Result delete(@PathVariable Long orderNo) {
        chargeOrderService.delete(orderNo);
        return Result.success();
    }
}
